#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <hiredis/hiredis.h>

int main(int argc, char** argv)
{
	char msg[600];
	char result[5000][13];
	char rest[50000][13];
	int count=0;
	int rest_count=0;
	int i=0;

	//int mode = atoi(argv[1]);

	FILE *f = fopen("data.log","r");
	char *tmp = NULL;

	redisContext *redis;
	redisReply *reply;

	redis = redisConnect("127.0.0.1", 6379);
	if(redis->err)
	{
		fprintf(stderr, "접속이안된다고!!!!\n");
		exit(0);
	}

	srand(time(NULL));


	//tmp=(char*)substring(msg, 3, 11);
	while(fgets(msg,sizeof(msg),f)!=NULL)
	{

		//nam
		// if(mode==3333)
		// {
		// 	tmp = (char *)substring(msg, 0, 4);
		// 	if(strncmp(tmp,"A0011",5)==0)
		// 	{
		// 		printf("%s",msg);
		// 	}
		// 	free(tmp);
		// 	continue;
		// }
		//nam

		tmp = (char*)substring(msg,0,4);

		if(strncmp(tmp,"A3011",5)==0)
		{
			free(tmp);
			tmp = (char*)substring(msg,5,16);
			for(i=0;i<count;i++)
			{
				if(strncmp(tmp,result[i],12)==0) break;
			}
			if(i==count)
			{
				strcpy(result[count++],tmp);
			}
			if(strncmp(tmp,"KR7005930003",12)==0)
			{
				printf("** 체결 **\n");

				printf("전일대비-%c",msg[18]);
				switch(msg[18])
				{
					case '1':
					printf("상한\n");
					break;
					case '2':
					printf("상승\n");
					break;
					case '3':
					printf("보합\n");
					break;
					case '4':
					printf("하한\n");
					break;
					case '5':
					printf("하락\n");
					break;
					case '0':
					printf("판단불가\n");
					break;
				}
				free(tmp);
				tmp = (char*)substring(msg, 19, 27);
				//printf("전일대비: %s\n", tmp);
				reply = redisCommand(redis, "PUBLISH EQ.2.4 %s", tmp);
				freeReplyObject(reply);

				free(tmp);
				tmp = (char*)substring(msg, 28, 36);
				//printf("체결가격: %s\n", tmp);
				reply = redisCommand(redis, "PUBLISH EQ.1.4 %s", tmp);
				freeReplyObject(reply);

				free(tmp);
				tmp = (char*)substring(msg, 37, 46);
				printf("체결수량: %s\n", tmp);

				free(tmp);
				tmp = (char*)substring(msg, 47, 48);
				printf("체결유형: %s, ", tmp);
				if(strncmp(tmp,"10",2)==0)
				{
					printf("시가단일가\n");
				}
				else if(strncmp(tmp,"11",2)==0)
				{
					printf("시가단일가연장\n");
				}
				else if(strncmp(tmp,"20",2)==0)
				{
					printf("장중단일가\n");
				}
				else if(strncmp(tmp,"21",2)==0)
				{
					printf("장중단일가연장\n");
				}
				else if(strncmp(tmp,"25",2)==0)
				{
					printf("Buy-In\n");
				}
				else if(strncmp(tmp,"30",2)==0)
				{
					printf("종가단일가\n");
				}
				else if(strncmp(tmp,"40",2)==0)
				{
					printf("접속\n");
				}
				else if(strncmp(tmp,"50",2)==0)
				{
					printf("장개시전시간외종가\n");
				}
				else if(strncmp(tmp,"60",2)==0)
				{
					printf("장종료후시간외종가개시\n");
				}
				else if(strncmp(tmp,"65",2)==0)
				{
					printf("장종료후시간외종가\n");
				}
				else if(strncmp(tmp,"70",2)==0)
				{
					printf("시간외단일가\n");
				}
				else if(strncmp(tmp,"80",2)==0)
				{
					printf("단위매매체결\n");
				}

				free(tmp);
				tmp = (char*)substring(msg, 49, 57);
				//printf("시    가: %s\n", tmp);
				reply = redisCommand(redis, "PUBLISH EQ.5.1 %s", tmp);
				freeReplyObject(reply);

				free(tmp);
				tmp = (char*)substring(msg, 58, 66);
				//printf("고    가: %s\n", tmp);
				reply = redisCommand(redis, "PUBLISH EQ.6.1 %s", tmp);
				freeReplyObject(reply);

				free(tmp);
				tmp = (char*)substring(msg, 67, 75);
				//printf("저    가: %s\n", tmp);
				reply = redisCommand(redis, "PUBLISH EQ.7.1 %s", tmp);
				freeReplyObject(reply);

				free(tmp);
				tmp = (char*)substring(msg, 76, 87);
				//printf("누적체결수량: %s\n", tmp);
				reply = redisCommand(redis, "PUBLISH EQ.4.4 %s", tmp);
				freeReplyObject(reply);

				free(tmp);
				tmp = (char*)substring(msg, 88, 105);
				printf("누적거래대금: %s\n", tmp);

				printf("최종매도매수구분: %s\n", msg[106]=='1'?"매도":"매수");

				free(tmp);
				tmp = (char*)substring(msg, 108, 113);
				//printf("체결시각: %s\n", tmp);
				reply = redisCommand(redis, "PUBLISH EQ.title %s", tmp);
				freeReplyObject(reply);

				free(tmp);
				tmp = (char*)substring(msg, 129, 137);
				printf("매도1호가: %s\n", tmp);

				free(tmp);
				tmp = (char*)substring(msg, 138, 146);
				printf("매수1호가: %s\n", tmp);
				usleep(rand()%5000);
			}
		}
		else if(strncmp(tmp,"B6011",5)==0)
		{
			free(tmp);
			tmp = (char*)substring(msg,5,16);
			for(i=0;i<rest_count;i++)
			{
				if(strncmp(tmp,rest[i],12)==0) break;
			}
			if(i==rest_count)
			{
				strcpy(rest[rest_count++],tmp);
			}
			if(strncmp(tmp,"KR7005930003",12)==0)
			{
				//printf("** 호가잔량 **\n");
				//printf("%s\n",msg);
				//printf("len:%d\n",strlen(msg));
				free(tmp);
				tmp = (char*)substring(msg,29,37);
				//printf("매도호가1: %s\t",tmp);
				reply = redisCommand(redis, "PUBLISH EQ.4.2 %s", tmp);
				freeReplyObject(reply);

				free(tmp);
				tmp = (char*)substring(msg,38,46);
				//printf("매수호가1: %s\n",tmp);
				reply = redisCommand(redis, "PUBLISH EQ.5.2 %s", tmp);
				freeReplyObject(reply);

				free(tmp);
				tmp = (char*)substring(msg,47,58);
				//printf("매도잔량1: %s\t",tmp);
				reply = redisCommand(redis, "PUBLISH EQ.4.1 %s", tmp);
				freeReplyObject(reply);

				free(tmp);
				tmp = (char*)substring(msg,59,70);
				//printf("매수잔량1: %s\n",tmp);
				reply = redisCommand(redis, "PUBLISH EQ.5.3 %s", tmp);
				freeReplyObject(reply);

				free(tmp);
				tmp = (char*)substring(msg,71,79);
				//printf("매도호가2: %s\t",tmp);
				reply = redisCommand(redis, "PUBLISH EQ.3.2 %s", tmp);
				freeReplyObject(reply);

				free(tmp);
				tmp = (char*)substring(msg,80,88);
				//printf("매수호가2: %s\n", tmp);
				reply = redisCommand(redis, "PUBLISH EQ.6.2 %s", tmp);
				freeReplyObject(reply);

				free(tmp);
				tmp = (char*)substring(msg,89,100);
				//printf("매도잔량2: %s\t",tmp);
				reply = redisCommand(redis, "PUBLISH EQ.3.1 %s", tmp);
				freeReplyObject(reply);

				free(tmp);
				tmp = (char*)substring(msg,101,112);
				//printf("매수잔량2: %s\n", tmp);
				reply = redisCommand(redis, "PUBLISH EQ.6.3 %s", tmp);
				freeReplyObject(reply);

				free(tmp);
				tmp = (char*)substring(msg,113,121);
				//printf("매도호가3: %s\t",tmp);
				reply = redisCommand(redis, "PUBLISH EQ.2.2 %s", tmp);
				freeReplyObject(reply);

				free(tmp);
				tmp = (char*)substring(msg,122,130);
				//printf("매수호가3: %s\n", tmp);
				reply = redisCommand(redis, "PUBLISH EQ.7.2 %s", tmp);
				freeReplyObject(reply);

				free(tmp);
				tmp = (char*)substring(msg,131,142);
				//printf("매도잔량3: %s\t",tmp);
				reply = redisCommand(redis, "PUBLISH EQ.2.1 %s", tmp);
				freeReplyObject(reply);

				free(tmp);
				tmp = (char*)substring(msg,143,154);
				//printf("매수잔량3: %s\n",tmp);
				reply = redisCommand(redis, "PUBLISH EQ.7.3 %s", tmp);
				freeReplyObject(reply);

				free(tmp);
				tmp = (char*)substring(msg,155,163);
				//printf("매도호가4: %s\t",tmp);
				reply = redisCommand(redis, "PUBLISH EQ.1.2 %s", tmp);
				freeReplyObject(reply);

				free(tmp);
				tmp = (char*)substring(msg,164,172);
				//printf("매수호가4: %s\n",tmp);
				reply = redisCommand(redis, "PUBLISH EQ.8.2 %s", tmp);
				freeReplyObject(reply);

				free(tmp);
				tmp = (char*)substring(msg,173,184);
				//printf("매도잔량4: %s\t",tmp);
				reply = redisCommand(redis, "PUBLISH EQ.1.1 %s", tmp);
				freeReplyObject(reply);

				free(tmp);
				tmp = (char*)substring(msg,185,196);
				//printf("매수잔량4: %s\n",tmp);
				reply = redisCommand(redis, "PUBLISH EQ.8.3 %s", tmp);
				freeReplyObject(reply);

				free(tmp);
				tmp = (char*)substring(msg,197,205);
				//printf("매도호가5: %s\t",tmp);
				reply = redisCommand(redis, "PUBLISH EQ.0.2 %s", tmp);
				freeReplyObject(reply);

				free(tmp);
				tmp = (char*)substring(msg,206,214);
				//printf("매수호가5: %s\n", tmp);
				reply = redisCommand(redis, "PUBLISH EQ.9.2 %s", tmp);
				freeReplyObject(reply);

				free(tmp);
				tmp = (char*)substring(msg,215,226);
				//printf("매도잔량5: %s\t",tmp);
				reply = redisCommand(redis, "PUBLISH EQ.0.1 %s", tmp);
				freeReplyObject(reply);

				free(tmp);
				tmp = (char*)substring(msg,227,238);
				//printf("매수잔량5: %s\n", tmp);
				reply = redisCommand(redis, "PUBLISH EQ.9.3 %s", tmp);
				freeReplyObject(reply);

				free(tmp);
				tmp = (char*)substring(msg,239,247);
				//printf("매도호가6: %s\t",tmp);

				free(tmp);
				tmp = (char*)substring(msg,248,256);
				//printf("매수호가6: %s\n", tmp);

				free(tmp);
				tmp = (char*)substring(msg,257,268);
				//printf("매도잔량6: %s\t",tmp);

				free(tmp);
				tmp = (char*)substring(msg,269,280);
				//printf("매수잔량6: %s\n", tmp);

				free(tmp);
				tmp = (char*)substring(msg,281,289);
				//printf("매도호가7: %s\t",tmp);

				free(tmp);
				tmp = (char*)substring(msg,290,298);
				//printf("매수호가7: %s\n", tmp);

				free(tmp);
				tmp = (char*)substring(msg,299,310);
				//printf("매도잔량7: %s\t",tmp);

				free(tmp);
				tmp = (char*)substring(msg,311,322);
				//printf("매수잔량7: %s\n", tmp);

				free(tmp);
				tmp = (char*)substring(msg,323,331);
				//printf("매도호가8: %s\t",tmp);

				free(tmp);
				tmp = (char*)substring(msg,332,340);
				//printf("매수호가8: %s\n", tmp);

				free(tmp);
				tmp = (char*)substring(msg,341,352);
				//printf("매도잔량8: %s\t",tmp);

				free(tmp);
				tmp = (char*)substring(msg,353,364);
				//printf("매수잔량8: %s\n", tmp);

				free(tmp);
				tmp = (char*)substring(msg,365,373);
				//printf("매도호가9: %s\t",tmp);

				free(tmp);
				tmp = (char*)substring(msg,374,382);
				//printf("매수호가9: %s\n", tmp);

				free(tmp);
				tmp = (char*)substring(msg,383,394);
				//printf("매도잔량9: %s\t",tmp);

				free(tmp);
				tmp = (char*)substring(msg,395,406);
				//printf("매수잔량9: %s\n", tmp);

				free(tmp);
				tmp = (char*)substring(msg,407,415);
				//printf("매도호가0: %s\t",tmp);

				free(tmp);
				tmp = (char*)substring(msg,416,424);
				//printf("매수호가0: %s\n", tmp);

				free(tmp);
				tmp = (char*)substring(msg,425,436);
				//printf("매도잔량0: %s\t",tmp);

				free(tmp);
				tmp = (char*)substring(msg,437,448);
				//printf("매수잔량0: %s\n", tmp);

				free(tmp);
				tmp = (char*)substring(msg,449,460);
				//printf("매도총잔: %s\t", tmp);
				reply = redisCommand(redis, "PUBLISH EQ.10.1 %s", tmp);
				freeReplyObject(reply);

				free(tmp);
				tmp = (char*)substring(msg,461,472);
				//printf("매수총잔: %s\n", tmp);
				reply = redisCommand(redis, "PUBLISH EQ.10.3 %s", tmp);
				freeReplyObject(reply);

				free(tmp);
				tmp = (char*)substring(msg,524,532);
				//printf("예상가  : %s\t", tmp);

				free(tmp);
				tmp = (char*)substring(msg,533,544);
				//printf("예상거래량: %s\n", tmp);
				
				usleep(rand()%5000);
			}
		}
		free(tmp);
	}
	fclose(f);
//	for(i=0;i<count;i++)
//		printf("%s\n",result[i]);
//	printf("%d datas\n",count);
//	for(i=0;i<rest_count;i++)
//		printf("%s\n",result[i]);
//	printf("%d rest_datas\n", rest_count);
	return 0;
}

